﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;

using Ivy.Common.Framework.Base;
using Ivy.Common.Framework.DataAccess;
using Ivy.SiteAdmin.Entity;

namespace Ivy.SiteAdmin.DataAccess
{
   public class SubscriptionMasterDao : IDaoInterface<SubscriptionMaster, SubscriptionMasterDao>
   {
       #region  Variable & constant
       #endregion  Variable & constant

       #region Constructor & Destructor
       #endregion Constructor & Destructor

       #region Properties
       #endregion Properties

       #region Public Methods
       #region  IDaoInterface<SubscriptionMaster, SubscriptionMasterDao> Members
       public bool Insert(SubscriptionMaster SubscriptionMasterEntity)
       {
           bool IsOperationSuccessful = false;
          
               if (SubscriptionMasterEntity.EntityState == EntityState.New)
               {
                   DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                   DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                   CmdObj.CommandText = "DasIdeen_SubscriptionMaster_Insert";
                   CmdObj.CommandType = CommandType.StoredProcedure;
                   DataAccessProviderUtility.AddParameter(CmdObj, "@SubscribItemCode", ParameterDirection.Input, DbType.Int32, SubscriptionMasterEntity.SubscribItemCode);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@SubscribItemDescription", ParameterDirection.Input, DbType.String, SubscriptionMasterEntity.SubscribItemDescription);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@SubscribUserMailID", ParameterDirection.Input, DbType.String, SubscriptionMasterEntity.SubscribUserMailID);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@SubscrbDate", ParameterDirection.Input, DbType.DateTime, SubscriptionMasterEntity.SubscrbDate);

                   DatabaseCommon.InsertStoredProcForParameterDatEntityBase(CmdObj, SubscriptionMasterEntity);

                   DataAccessProviderUtility.AddParameter(CmdObj, "@SubscribKey", ParameterDirection.Output, DbType.Int32, DBNull.Value);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfCreation", ParameterDirection.Output, DbType.DateTime, DBNull.Value);
                   CmdObj.Connection = DbConn;
                   DbConn.Open();
                   CmdObj.Prepare();
                   int _NoOfRecord = CmdObj.ExecuteNonQuery();
                   if (_NoOfRecord == 1)
                   {
                       SubscriptionMasterEntity.SubscribKey = Convert.ToInt32(CmdObj.Parameters["@SubscribKey"].Value);
                       SubscriptionMasterEntity.Sys_DateOfCreation = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfCreation"].Value);
                       SubscriptionMasterEntity.Sys_DateOfModification = SubscriptionMasterEntity.Sys_DateOfCreation;
                       SubscriptionMasterEntity.EntityState = EntityState.UnChanged;
                       IsOperationSuccessful = true;
                   }
                   CmdObj.Dispose();
                   DbConn.Close();
               }
               else
               {

                   throw new Exception("For Inserting new User entity data to database,[EntityState] attribute should be [New]");
               }
           
           return IsOperationSuccessful;
       }

       public bool Update(SubscriptionMaster SubscriptionMasterEntity)
       {
           bool IsOperationSuccessful = false;
           
               if (SubscriptionMasterEntity.EntityState == EntityState.Changed)
               {
                   DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                   DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                   CmdObj.CommandText = "DasIdeen_SubscriptionMaster_Update";
                   CmdObj.CommandType = CommandType.StoredProcedure;

                   DataAccessProviderUtility.AddParameter(CmdObj, "@SubscribKey", ParameterDirection.Input, DbType.Int32, SubscriptionMasterEntity.SubscribKey);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@SubscribItemCode", ParameterDirection.Input, DbType.Int32, SubscriptionMasterEntity.SubscribItemCode);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@SubscribItemDescription", ParameterDirection.Input, DbType.String, SubscriptionMasterEntity.SubscribItemDescription);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@SubscribUserMailID", ParameterDirection.Input, DbType.String, SubscriptionMasterEntity.SubscribUserMailID);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@SubscrbDate", ParameterDirection.Input, DbType.DateTime, SubscriptionMasterEntity.SubscrbDate);

                   DatabaseCommon.UpdateStoredProcForParameterDatEntityBase(CmdObj, SubscriptionMasterEntity);

                   DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfModification", ParameterDirection.Output, DbType.DateTime, DBNull.Value);

                   CmdObj.Connection = DbConn;
                   DbConn.Open();
                   CmdObj.Prepare();
                   int _NoOfRecords = CmdObj.ExecuteNonQuery();
                   if (_NoOfRecords == 1)
                   {
                       SubscriptionMasterEntity.Sys_DateOfModification = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfModification"].Value);
                       SubscriptionMasterEntity.EntityState = EntityState.UnChanged;
                       IsOperationSuccessful = true;
                   }

                   CmdObj.Dispose();
                   DbConn.Close();
               }
               else
               {
                   throw new Exception("For Updating existing SubscriptionMaster entity data to database, [EntityState] attribute should be [Changed]");
               }
           
           return IsOperationSuccessful;
       }

       public bool Delete(SubscriptionMaster DataEntity)
       {
           throw new NotImplementedException();
       }

       public SubscriptionMaster GetByID(string ID)
       {
           throw new NotImplementedException();
       }

       public List<SubscriptionMaster> GetAll()
       {
           List<SubscriptionMaster> lSubscriptionMasterList = new List<SubscriptionMaster>();
          
               DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
               DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
               CmdObj.CommandText = "DasIdeen_SubscriptionMaster_GetAll";
               CmdObj.CommandType = CommandType.StoredProcedure;

               CmdObj.Connection = DbConn;
               DbConn.Open();
               CmdObj.Prepare();

               DbDataReader _DataReader = CmdObj.ExecuteReader();
               if (_DataReader.HasRows)
               {
                   while (_DataReader.Read())
                   {
                       var lSubscriptionMastertemp = new SubscriptionMaster(); //IMPORTANT NOTE:- Create instance only when required.
                       this.TranslateSubscriptionMasterFieldsFromReaderToEntity(lSubscriptionMastertemp, _DataReader);
                       lSubscriptionMasterList.Add(lSubscriptionMastertemp);
                   }
               }
               CmdObj.Dispose();
               DbConn.Close();
          

           return lSubscriptionMasterList;
       }

       public List<SubscriptionMaster> GetByCriteria(SubscriptionMasterDao CriteriaEntity)
       {
           throw new NotImplementedException();
       }
       public SubscriptionMaster GetByKey(int SubscribKey)
       {
            {
                SubscriptionMaster _SubscriptionMasterList = new SubscriptionMaster(); //IMPORTANT NOTE:- Just declare the variable never create an instance, create instance only when required.
           
               DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
               DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
               CmdObj.CommandText = "DasIdeen_SubscriptionMaster_GetByKey";
               CmdObj.CommandType = CommandType.StoredProcedure;

               DataAccessProviderUtility.AddParameter(CmdObj, "@SubscribKey", ParameterDirection.Input, DbType.Int32, Convert.ToInt32(SubscribKey));


               CmdObj.Connection = DbConn;
               DbConn.Open();
               CmdObj.Prepare();
               DbDataReader _DataReader = CmdObj.ExecuteReader();

               if (_DataReader.HasRows)
               {
                   if (_DataReader.Read())
                   {
                       _SubscriptionMasterList = new SubscriptionMaster(); //IMPORTANT NOTE:- Create instance only when required.
                       TranslateSubscriptionMasterFieldsFromReaderToEntity(_SubscriptionMasterList, _DataReader);
                   }
               }
               else
               {
                   throw new Exception("No _SubscriptionMaster found");
               }
               CmdObj.Dispose();
               DbConn.Close();
           
           return _SubscriptionMasterList;
       }
       }
       #endregion

       #endregion Public Methods

       #region Events
       #endregion Events

       #region Private Methods

       private void TranslateSubscriptionMasterFieldsFromReaderToEntity(SubscriptionMaster pSubscriptionMaster, DbDataReader pDbDataReader)
       {
          
               pSubscriptionMaster.SubscribKey = Convert.ToInt32(pDbDataReader["SubscribKey"]);
               pSubscriptionMaster.SubscribItemCode = Convert.ToInt32(pDbDataReader["SubscribItemCode"]);
               pSubscriptionMaster.SubscribItemDescription = Convert.ToString(pDbDataReader["SubscribItemDescription"]);
               pSubscriptionMaster.SubscribUserMailID = Convert.ToString(pDbDataReader["SubscribUserMailID"]);
               pSubscriptionMaster.SubscrbDate = Convert.ToDateTime(pDbDataReader["SubscrbDate"]);
               pSubscriptionMaster.Sys_DateOfCreation = Convert.ToDateTime(pDbDataReader["Sys_DateOfCreation"]);

               DatabaseCommon.TranslateSystemFieldsFromReaderToEntity(pSubscriptionMaster, pDbDataReader);
           
       }
       #endregion Private Methods

   }
}
